SUBROUTINE prob_ic_emax(ageN)
  USE commonvars
  IMPLICIT NONE
  INCLUDE 'mpif.h'

  INTEGER, INTENT(IN) :: ageN
  INTEGER             :: ifu, ipr, elap, ifine,  ia, iedu, age, iwealth
  REAL(8)             :: probage

  !We compute the probabilities to use in emaxtemp here so that we can do it only once
  ALLOCATE(probqcons_c(ageN,Neduc,Nwealth,Nab,Nfines,Npr,Nfunds,Nelap), probqcons_i(Nfines,Npr,Nfunds,Nab,Nelap))
  
  probqcons_c = 0d0
  probqcons_i = 0d0
  probage=(1d0/DBLE(ageN))
  
  ! We compute the probability of observing a particular mayor; it is needed to compute expected ln qcons
  ! We compute the probability if a challenger wins the elections; usefull for loops 2 and 4
  ! We divide the features we use to determine the production of qcons in features that affect the production and the elections
  ! iq (past public consumption), itheta (rel contributions), and isx (stealing > x%) affect only the probability of reelection
  ! iterm is only relevant because, if iterm = Nnterms, the prob that the incumbents is reelected = 0
  ! Features affecting the production
  ! For a challanger ipast = 1, we can therefore get rid of it
  DO elap = 1, Nelap
     DO ifu = 1, Nfunds
        DO ipr = 1, Npr
           DO ifine = 1, Nfines
              DO ia = 1, Nab
                 DO iwealth = 1, Nwealth
                    DO iedu = 1, Neduc
                       DO age = 1, ageN

                          ! Here we only consider the probability of observing the random characteristics of the challenger
                          ! That affect the production of qcons. The characteristics of the incumbent affecting the elections are considered in probemax
                          probqcons_c(age,iedu,iwealth,ia,ifine,ipr,ifu,elap) = probage*probedu(iedu)*nonpardist_wealth_fu_pr(iwealth,ifu,ipr)*pability(ia)*pfine(ifine)*pappeal(elap)

                       END DO
                    END DO
                 END DO
              END DO
           END DO
        END DO
     END DO
  END DO

  ! We compute the probability if an incumbent wins the elections; usefull for loops 3
  ! We divide the features we use to determine the production of qcons in features that affect the production and the elections
  ! iq (past public consumption), itheta (rel contributions), and isx (stealing > x%) affect only the probability of reelection
  ! iterm is only relevant because, if iterm = Nnterms, the prob that the incumbents is reelected = 0
  ! Features affecting the production
  DO elap = 1, Nelap
     DO ia = 1, Nab
        DO ifine = 1, Nfines
           DO ifu = 1, Nfunds
              DO ipr = 1, Npr
                 
                 ! Here we only consider the probability of observing the random characteristics of the incumbent
                 ! That affect the production of qcons. The ones affecting the elections are considered in probemax
                 ! The only variables that are random if an incumbent wins are ipr,ifu,ifine
                 probqcons_i(ifine,ipr,ifu,ia,elap) = pfine(ifine)*SUM(nonpardist_wealth_fu_pr(:,ifu,ipr))*pability(ia)*pappeal(elap)
                 
              END DO
           END DO
        END DO
     END DO
  END DO

END SUBROUTINE prob_ic_emax
